<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="订单id" prop="orderId">
      <el-input v-model="dataForm.orderId" placeholder="订单id"></el-input>
    </el-form-item>
    <el-form-item label="运单id" prop="tranOrderId">
      <el-input v-model="dataForm.tranOrderId" placeholder="运单id"></el-input>
    </el-form-item>
    <el-form-item label="货物类型id" prop="goodsTypeId">
      <el-input v-model="dataForm.goodsTypeId" placeholder="货物类型id"></el-input>
    </el-form-item>
    <el-form-item label="货物名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="货物名称"></el-input>
    </el-form-item>
    <el-form-item label="货物单位" prop="unit">
      <el-input v-model="dataForm.unit" placeholder="货物单位"></el-input>
    </el-form-item>
    <el-form-item label="货品货值" prop="cargoValue">
      <el-input v-model="dataForm.cargoValue" placeholder="货品货值"></el-input>
    </el-form-item>
    <el-form-item label="货品条码" prop="cargoBarcode">
      <el-input v-model="dataForm.cargoBarcode" placeholder="货品条码"></el-input>
    </el-form-item>
    <el-form-item label="货品数量" prop="quantity">
      <el-input v-model="dataForm.quantity" placeholder="货品数量"></el-input>
    </el-form-item>
    <el-form-item label="货品体积" prop="volume">
      <el-input v-model="dataForm.volume" placeholder="货品体积"></el-input>
    </el-form-item>
    <el-form-item label="货品重量" prop="weight">
      <el-input v-model="dataForm.weight" placeholder="货品重量"></el-input>
    </el-form-item>
    <el-form-item label="货品备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="货品备注"></el-input>
    </el-form-item>
    <el-form-item label="货品总体积" prop="totalVolume">
      <el-input v-model="dataForm.totalVolume" placeholder="货品总体积"></el-input>
    </el-form-item>
    <el-form-item label="货品总重量" prop="totalWeight">
      <el-input v-model="dataForm.totalWeight" placeholder="货品总重量"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          orderId: '',
          tranOrderId: '',
          goodsTypeId: '',
          name: '',
          unit: '',
          cargoValue: '',
          cargoBarcode: '',
          quantity: '',
          volume: '',
          weight: '',
          remark: '',
          totalVolume: '',
          totalWeight: ''
        },
        dataRule: {
          orderId: [
            { required: true, message: '订单id不能为空', trigger: 'blur' }
          ],
          tranOrderId: [
            { required: true, message: '运单id不能为空', trigger: 'blur' }
          ],
          goodsTypeId: [
            { required: true, message: '货物类型id不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '货物名称不能为空', trigger: 'blur' }
          ],
          unit: [
            { required: true, message: '货物单位不能为空', trigger: 'blur' }
          ],
          cargoValue: [
            { required: true, message: '货品货值不能为空', trigger: 'blur' }
          ],
          cargoBarcode: [
            { required: true, message: '货品条码不能为空', trigger: 'blur' }
          ],
          quantity: [
            { required: true, message: '货品数量不能为空', trigger: 'blur' }
          ],
          volume: [
            { required: true, message: '货品体积不能为空', trigger: 'blur' }
          ],
          weight: [
            { required: true, message: '货品重量不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '货品备注不能为空', trigger: 'blur' }
          ],
          totalVolume: [
            { required: true, message: '货品总体积不能为空', trigger: 'blur' }
          ],
          totalWeight: [
            { required: true, message: '货品总重量不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/oms/ordercargo/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.orderId = data.orderCargo.orderId
                this.dataForm.tranOrderId = data.orderCargo.tranOrderId
                this.dataForm.goodsTypeId = data.orderCargo.goodsTypeId
                this.dataForm.name = data.orderCargo.name
                this.dataForm.unit = data.orderCargo.unit
                this.dataForm.cargoValue = data.orderCargo.cargoValue
                this.dataForm.cargoBarcode = data.orderCargo.cargoBarcode
                this.dataForm.quantity = data.orderCargo.quantity
                this.dataForm.volume = data.orderCargo.volume
                this.dataForm.weight = data.orderCargo.weight
                this.dataForm.remark = data.orderCargo.remark
                this.dataForm.totalVolume = data.orderCargo.totalVolume
                this.dataForm.totalWeight = data.orderCargo.totalWeight
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/oms/ordercargo/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'orderId': this.dataForm.orderId,
                'tranOrderId': this.dataForm.tranOrderId,
                'goodsTypeId': this.dataForm.goodsTypeId,
                'name': this.dataForm.name,
                'unit': this.dataForm.unit,
                'cargoValue': this.dataForm.cargoValue,
                'cargoBarcode': this.dataForm.cargoBarcode,
                'quantity': this.dataForm.quantity,
                'volume': this.dataForm.volume,
                'weight': this.dataForm.weight,
                'remark': this.dataForm.remark,
                'totalVolume': this.dataForm.totalVolume,
                'totalWeight': this.dataForm.totalWeight
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
